/**
 * Copyright 2015-2016 eyaomall.cn
 * All rights reserved.
 *
 * @project
 * @author jackey
 * @version 1.0
 * @date 2016-01-06
 */
package com.eyaomall.wxmall.common.helper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.eyaomall.wxmall.common.enums.EmployeeEnums;
import com.lycheeframework.core.common.cookie.SessionManager;
import com.lycheeframework.core.common.util.StringUtil;
import com.lycheeframework.core.common.util.encrypt.MD5;
import com.lycheeframework.core.common.util.encrypt.SHA256;

public class EmployeeHelper {
	private static boolean flag = false;
	/**
	 * 获取会员ID
	 * @return
	 */
	public static Integer getId(){
		if(flag){
			return 10;
		}
		
		String memberId = (String) getManager().getValue(EmployeeEnums._EMPLOYEE_FROM.getName(),EmployeeEnums._EMPLOYEE_FROM_ID.getName(),true);
		
		return StringUtil.isEmpty(memberId) ? 0 : Integer.parseInt(memberId);
	}
	
	/**
	 * 获取会员OpenID
	 * @return
	 */
	public static Integer getStoreId(){
		if(flag){
			return 10;
		}
		
		String storeId = (String) getManager().getValue(EmployeeEnums._EMPLOYEE_FROM.getName(),EmployeeEnums._EMPLOYEE_FROM_SHORES.getName(),true);
		if(StringUtil.isEmpty(storeId) || "null".equalsIgnoreCase(storeId)){
			return 0;
		}
		
		return StringUtil.isEmpty(storeId) ? 0 : Integer.parseInt(storeId);
	}
	
	/**
	 * 获取会员手机号码
	 * @return
	 */
	public static String getPhone(){
		if(flag){
			return "18525336892";
		}
		
		return getManager().getValue(EmployeeEnums._EMPLOYEE_FROM.getName(), EmployeeEnums._EMPLOYEE_FROM_PHONE.getName(),true);
	}
	
	/**
	 * 获取会员昵称
	 * @return
	 */
	public static String getName(){
		if(flag){
			return "姜冬梅";
		}
		
		return (String) getManager().getValue(EmployeeEnums._EMPLOYEE_FROM.getName(),EmployeeEnums._EMPLOYEE_FROM_NAME.getName(),true);
	}
	
	
	/**
	 * 获取店铺名称
	 * @return
	 */
	public static String getStoreName(){
		
		return (String) getManager().getValue(EmployeeEnums._EMPLOYEE_FROM.getName(),EmployeeEnums._EMPLOYEE_FROM_SHORES_NAME.getName(),true);
	}
	
	/**
	 * 加密密码明文
	 * @param passwd
	 * @return
	 */
	public static String getEntryPasswd(String passwd){
		passwd = SHA256.encrypt(passwd);
		
		return MD5.encryptMD5(passwd);
	}

	public static HttpServletRequest getRequest() {
		
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static HttpServletResponse getResponse() {
		
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
	}
	
	private static SessionManager getManager(){
		
		return new SessionManager(getRequest());
	}
	
	
	public static void cleanAll(){
		getRequest().getSession().invalidate();
	}
}
